package com.lyf.service.user.Impl;

import com.lyf.context.BaseContext;
import com.lyf.dto.ShoppingCartDTO;
import com.lyf.mapper.admin.DishMapper;
import com.lyf.mapper.admin.SetmealMapper;
import com.lyf.mapper.user.ShoppingCartMapper;
import com.lyf.pojo.Dish;
import com.lyf.pojo.Setmeal;
import com.lyf.pojo.ShoppingCart;
import com.lyf.service.user.ShoppingCartService;
import com.lyf.vo.DishItemVO;
import com.lyf.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 获取当前用户
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();

        ShoppingCart temp_shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,temp_shoppingCart);
        temp_shoppingCart.setUserId(userId);

        // 判断当前菜品或套餐是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(temp_shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() > 0){
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            // 存在，获取当前数量
            Integer number = shoppingCart.getNumber();
            // 得到单价
            BigDecimal unitAmount = shoppingCart.getAmount().divide(new BigDecimal(shoppingCart.getNumber()), 2, BigDecimal.ROUND_HALF_UP);
            // 设置数量增加1
            shoppingCart.setNumber(number + 1);
            shoppingCartMapper.update(shoppingCart);
            return;
        }

        ShoppingCart shoppingCart = new ShoppingCart();
        // 不存在，添加到购物车
        if (dishId != null){
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart = ShoppingCart.builder()
                    .name(dish.getName())
                    .image(dish.getImage())
                    .userId(userId)
                    .dishId(dishId)
                    .setmealId(setmealId)
                    .dishFlavor(dishFlavor)
                    .number(1)
                    .amount(dish.getPrice())
                    .createTime(LocalDateTime.now())
                    .build();
        }
        // 添加套餐信息
        if (setmealId != null){
            SetmealVO setmeal = setmealMapper.selectById(setmealId);

            shoppingCart = ShoppingCart.builder()
                    .name(setmeal.getName())
                    .image(setmeal.getImage())
                    .userId(userId)
                    .dishId(dishId)
                    .setmealId(setmealId)
                    .dishFlavor(dishFlavor)
                    .number(1)
                    .amount(setmeal.getPrice())
                    .createTime(LocalDateTime.now())
                    .build();
        }
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        ShoppingCart shoppingCart = shoppingCartMapper.getByDishIdOrSetmealId(userId,dishId,setmealId);
        if (shoppingCart.getNumber() <= 1){
            shoppingCartMapper.deleteById(shoppingCart);
            return;
        }
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.update(shoppingCart);
    }

    /**
     * 清空购物车
     */
    public void clean() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.deleteById(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }
}